package com.abc.common.util;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Describe:
 */
public class StringUtils extends org.apache.commons.lang.StringUtils {

    /**
     * 去除字符串前后全角和半角空格
     *
     * @param tarStr
     * @return
     */
    public static String trimSbc(String tarStr) {
        if (isEmpty(tarStr)) return tarStr;
        tarStr = tarStr.trim();
        while (tarStr.startsWith("　")) {
            tarStr = tarStr.substring(1, tarStr.length()).trim();
        }
        while (tarStr.endsWith("　")) {
            tarStr = tarStr.substring(0, tarStr.length() - 1).trim();
        }
        return tarStr;
    }





    /**
     * Method stringReplace ...
     * 替换字符串
     *
     * @param sourceString    of type String
     * @param toReplaceString of type String
     * @param replaceString   of type String
     * @return String
     */
    public static String stringReplace(String sourceString, String toReplaceString, String replaceString) {
        String returnString = sourceString;
        int stringLength = 0;
        if (toReplaceString != null) {
            stringLength = toReplaceString.length();
        }
        if (returnString != null && returnString.length() >= stringLength) {
            int max = 0;
            String S4 = " ";
            for (int i = 0; i < sourceString.length(); i++) {
                max = i + toReplaceString.length() > sourceString.length() ? sourceString.length() : i + stringLength;
                String S3 = sourceString.substring(i, max);
                if (!S3.equals(toReplaceString)) {
                    S4 += S3.substring(0, 1);
                } else {
                    S4 += replaceString;
                    i += stringLength - 1;
                }
            }
            returnString = S4;
        }
        return returnString;
    }
    /**
     * 随机生成指定长度的字符串
     * @param length
     * @return
     */
    public static String randomString(int length)
    {
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer buf = new StringBuffer();

        for(int i = 0 ;i < length ; i ++)
        {
            int num = random.nextInt(62);
            buf.append(str.charAt(num));
        }

        return buf.toString();
    }
}
